package com.region.integral.business_user.obtain.dao.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.region.integral.business_user.obtain.dao.IIntegralGoodsCommented;
import com.region.integral.business_user.pojo.BUIEntity;
import com.region.integral.business_user.xml.Regulation;
import com.region.integral.business_user.xml.dto.CheckYes;
import com.region.integral.business_user.xml.dto.GoodsCommented;
import com.region.integral.common_user.obtain.vo.IntegralData;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.BusinessUserLogUtil;
import com.region.integral.util.XMLRegulationUtil;
@Component("iIntegralGoodsCommented")
public class IntegralGoodsCommentedImpl extends BaseHibernate implements IIntegralGoodsCommented {

	@Override
	public void goodsCommented(int userid, boolean isbuyer, int goodNum,
			int badNum, IntegralData integralData) throws IntegralException {
		BUIEntity buiEntity = getBUIEntity(userid);
		if (buiEntity != null) {
			Regulation regulation = XMLRegulationUtil.unmarshaller2();
			List<GoodsCommented> goodsCommenteds = regulation.getGoodscommented();
			for (GoodsCommented goodsCommented : goodsCommenteds) {
				if(isbuyer && StringUtils.isNotEmpty(goodsCommented.getBuyer())){
					int currIntegral = buiEntity.getCurrIntegral();
					int maxIntegral = buiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ goodsCommented.getIntegral();
					buiEntity.setCurrIntegral(totalIntegral);
					if (maxIntegral < totalIntegral) {
						buiEntity.setMaxIntegral(totalIntegral);
					}
					integralData.setSuccee(true);
					hibernateTemplate.update(buiEntity);
					hibernateTemplate.save(BusinessUserLogUtil.log(userid,goodsCommented.getIntegral(), "goodsCommented"));
				}
				if (goodNum!=0 &&goodsCommented.getRate()!=0&& goodNum % goodsCommented.getRate()==0) {
					int currIntegral = buiEntity.getCurrIntegral();
					int maxIntegral = buiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ goodsCommented.getIntegral();
					buiEntity.setCurrIntegral(totalIntegral);
					if (maxIntegral < totalIntegral) {
						buiEntity.setMaxIntegral(totalIntegral);
					}
					integralData.setSuccee(true);
					hibernateTemplate.update(buiEntity);
					hibernateTemplate.save(BusinessUserLogUtil.log(userid,goodsCommented.getIntegral(), "goodsCommented"));
				}
				if(goodsCommented.getBelowPercent()!=0&&(goodNum+badNum)>goodsCommented.getOver()&&
						(new BigDecimal(goodNum)).divide(new BigDecimal((goodNum+badNum)), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).intValue() >goodsCommented.getBelowPercent()
						&& (new BigDecimal(goodNum)).divide(new BigDecimal((goodNum+badNum)), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).intValue() <goodsCommented.getHighPercent()){
					int currIntegral = buiEntity.getCurrIntegral();
					int maxIntegral = buiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ goodsCommented.getIntegral();
					buiEntity.setCurrIntegral(totalIntegral);
					if (maxIntegral < totalIntegral) {
						buiEntity.setMaxIntegral(totalIntegral);
					}
					integralData.setSuccee(true);
					hibernateTemplate.update(buiEntity);
					hibernateTemplate.save(BusinessUserLogUtil.log(userid,goodsCommented.getIntegral(), "goodsCommented"));
				}
				if(goodsCommented.getHighPercent()!=0&&goodsCommented.getBelowPercent()==0&&(goodNum+badNum)>goodsCommented.getOver()&&
						 (new BigDecimal(goodNum)).divide(new BigDecimal((goodNum+badNum)), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).intValue() >goodsCommented.getHighPercent()){
					int currIntegral = buiEntity.getCurrIntegral();
					int maxIntegral = buiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ goodsCommented.getIntegral();
					buiEntity.setCurrIntegral(totalIntegral);
					if (maxIntegral < totalIntegral) {
						buiEntity.setMaxIntegral(totalIntegral);
					}
					integralData.setSuccee(true);
					hibernateTemplate.update(buiEntity);
					hibernateTemplate.save(BusinessUserLogUtil.log(userid,goodsCommented.getIntegral(), "goodsCommented"));
				}
			}
		}
		
		
	}

}
